﻿<%@ Page Language="C#" %>

<!DOCTYPE html>
<script runat="server">


    /// <summary>
    /// SqlSugarClient操作数据库是线程安的可以单例
    /// </summary>
    /// <param name="name">读/写</param>
    /// <returns></returns>
    public static SqlSugar.SqlSugarClient dbHelper()
    {
        //创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
        //可以在连接字符串中设置连接池pooling=true;表示开启连接池
        //eg:min pool size=2;MaxPoolSize=4;表示最小连接池为2，最大连接池是4；默认是100
        SqlSugar.SqlSugarClient DB = new SqlSugar.SqlSugarClient(new SqlSugar.ConnectionConfig()
        {
            DbType = SqlSugar.DbType.MySql,
            ConnectionString = "server=rm-2ze29it63mpgq7l0iuo.mysql.rds.aliyuncs.com;port=3306;database=base;uid=wangshuyu01;pwd=WangShuYu01@654321;CharSet=utf8mb4;Allow User Variables=True;sslMode=None;",
            IsAutoCloseConnection = true,
            InitKeyType = SqlSugar.InitKeyType.Attribute //从实体特性中读取主键自增列信息
        });
        return DB;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind_data();
        }
    }

    private void DataBind_data()
    {
        var list = dbHelper().Ado.SqlQuery<dynamic>("SELECT * FROM " + Request["tn"].ToString() + " WHERE id = '" + Request["id"].ToString() + "'");
        foreach (var item in list)
        {
            标识Literal.Text = item.id.ToString();
            父标识Literal.Text = item.pid.ToString();
            级别Literal.Text = item.grade.ToString();
            叶子Literal.Text = item.leaf.ToString();

            层次Literal.Text = item.levels.ToString();
            名称TextBox.Text = item.name.ToString();
            链接TextBox.Text = item.url;
            值TextBox.Text = item.value;

            setDropDownList(禁用DropDownList, item.disable.ToString(), "否");
            setDropDownList(节点状态DropDownList, item.state.ToString(), "open");

            if (item.leaf.ToString().ToLower() == "true")
            {
                节点状态DropDownList.Enabled = false;
            }
        }
    }

    private void setDropDownList(DropDownList list, string value, string defaultValue)
    {
        try
        {
            list.Items.FindByValue(value).Selected = true;
        }
        catch
        {
            //list.Items.FindByValue(defaultValue).Selected = true;
        }
    }

    protected void 重置Button_Click(object sender, EventArgs e)
    {
        DataBind_data();
    }

    protected void 提交Button_Click(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlParameter[] P = {
                                    new System.Data.SqlClient.SqlParameter("@id", Request["id"].ToString()),
                                    new System.Data.SqlClient.SqlParameter("@pid", 父标识Literal.Text),
                                    new System.Data.SqlClient.SqlParameter("@grade", 级别Literal.Text),
                                    new System.Data.SqlClient.SqlParameter("@leaf", 叶子Literal.Text),
                                    new System.Data.SqlClient.SqlParameter("@levels", 层次Literal.Text),
                                    new System.Data.SqlClient.SqlParameter("@name", 名称TextBox.Text),
                                    new System.Data.SqlClient.SqlParameter("@url", 链接TextBox.Text),
                                    new System.Data.SqlClient.SqlParameter("@disable", 禁用DropDownList.Text) ,
                                    new System.Data.SqlClient.SqlParameter("@state", 节点状态DropDownList.Text),
                                    new System.Data.SqlClient.SqlParameter("@value", 值TextBox.Text)
                                };


        var dc = new System.Collections.Generic.Dictionary<string, object>();
        dc.Add("id", Request["id"].ToString());
        dc.Add("pid", 父标识Literal.Text);
        dc.Add("grade", 级别Literal.Text);
        dc.Add("leaf", 叶子Literal.Text);
        dc.Add("levels", 层次Literal.Text);
        dc.Add("name", 名称TextBox.Text);
        dc.Add("url", 链接TextBox.Text);
        dc.Add("disable", 禁用DropDownList.Text);
        dc.Add("state", 节点状态DropDownList.Text);
        dc.Add("value", 值TextBox.Text);
        dbHelper().Updateable(dc).AS(Request["tn"].ToString().ToLower()).WhereColumns("id").ExecuteCommand();

        //if (Request["tn"].ToString() == "system_menu")
        //{
        //    dbHelper().Ado.ExecuteCommand(@"UPDATE " + Request["tn"].ToString() + @"
        //                            SET pid=@pid, grade=@grade, leaf=@leaf, levels=@levels, name=@name, url=@url,disable=@disable,state=@state,value=@value
        //                            WHERE (id = @id)", P);
        //}
        //else
        //{
        //    dbHelper().Ado.ExecuteCommand(@"UPDATE " + Request["tn"].ToString() + @"
        //                            SET pid=@pid, grade=@grade, leaf=@leaf, levels=@levels, name=@name,url=@url,disable=@disable,state=@state,value=@value
        //                            WHERE (id = @id)", P);
        //}

        //更新父级页面节点名称
        JSLiteral.Text = "<script>";
        JSLiteral.Text += "var node = parent.$(\"#tree\", window.parent.document).tree(\"find\", \"" + Request["id"].ToString() + "\");";
        JSLiteral.Text += "node.text = \"" + 名称TextBox.Text + "\";";
        JSLiteral.Text += "parent.$(\"#tree\", window.parent.document).tree(\"update\", node);";
        JSLiteral.Text += "</" + "script>";

        DataBind_data();
    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>树属性管理</title>
    <style type="text/css">
        .btnCancel { margin-left: 6px; }
        .input { width: 450px; padding: 3px; }
        .icons { float: left; margin: 3px; border: 2px solid #F2F7FC; }
        .iconsxz { float: left; margin: 3px; border: 2px solid red; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table cellspacing="10" cellpadding="0" border="0" style="background-color: #F2F7FC; border: #CCE0F5 solid 1px; font-size: 12px; width: 100%;">
            <tr style="background-color: #F2F7FC;">
                <td colspan="2">
                    <table width="100%">
                        <tr>
                            <td width="100px" align="right">标识：</td>
                            <td>
                                <asp:Literal ID="标识Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">父标识：</td>
                            <td>
                                <asp:Literal ID="父标识Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">叶子：</td>
                            <td>
                                <asp:Literal ID="叶子Literal" runat="server" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">级别：</td>
                            <td>
                                <asp:Literal ID="级别Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">层次：</td>
                            <td>
                                <asp:Literal ID="层次Literal" runat="server" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">名称：</td>
                            <td>
                                <asp:TextBox ID="名称TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">值：</td>
                            <td>
                                <asp:TextBox ID="值TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">链接：</td>
                            <td>
                                <asp:TextBox ID="链接TextBox" runat="server" CssClass="input" Width="600px" /></td>
                        </tr>
                        <tr style="display: none;">
                            <td width="100px" align="right">禁用：</td>
                            <td>
                                <asp:DropDownList ID="禁用DropDownList" runat="server">
                                    <asp:ListItem>是</asp:ListItem>
                                    <asp:ListItem>否</asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right">节点状态：</td>
                            <td>
                                <asp:DropDownList ID="节点状态DropDownList" runat="server">
                                    <asp:ListItem Text="关闭" Value="closed"></asp:ListItem>
                                    <asp:ListItem Text="打开" Value="open"></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td width="100px" align="right"></td>
                            <td>
                                <asp:Button ID="重置Button" runat="server" Text="重 置" OnClick="重置Button_Click" />
                                &nbsp;<asp:Button ID="提交Button" runat="server" Text="提 交" OnClick="提交Button_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <asp:Literal ID="JSLiteral" runat="server" />
    </form>
</body>
</html>
